package cn.stylefeng.guns.modular.business.service.impl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.business.entity.AdviceFeedback;
import cn.stylefeng.guns.modular.business.entity.CAdviceFeedback;
import cn.stylefeng.guns.modular.business.mapper.CAdviceFeedbackMapper;
import cn.stylefeng.guns.modular.business.model.params.CAdviceFeedbackParam;
import cn.stylefeng.guns.modular.business.model.result.CAdviceFeedbackResult;
import cn.stylefeng.guns.modular.business.service.CAdviceFeedbackService;
import cn.stylefeng.guns.sys.modular.system.entity.User;
import cn.stylefeng.guns.sys.modular.system.service.UserService;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author pzp
 * @since 2019-07-25
 */
@Service
public class CAdviceFeedbackServiceImpl extends ServiceImpl<CAdviceFeedbackMapper, CAdviceFeedback> implements CAdviceFeedbackService {

    @Autowired
    private UserService userService;

    /**
     * 新增意见反馈
     *
     * @param userId  用户id
     * @param email   邮箱
     * @param content 内容
     * @param picture 图片
     * @return
     */
    @Override
    public boolean add(Integer userId, String email, String content, String picture) {
        CAdviceFeedback adviceFeedback=new CAdviceFeedback();
        adviceFeedback.setUserId(userId);
        adviceFeedback.setContent(content);
        adviceFeedback.setPicture(picture);
        adviceFeedback.setEmail(email);
        adviceFeedback.setCreateTime(new Date());
        adviceFeedback.setUpdateTime(new Date());
        adviceFeedback.setDeleteFlag(0);
        return this.save(adviceFeedback);
    }

    @Override
    public void add(CAdviceFeedbackParam param){
        CAdviceFeedback entity = getEntity(param);
        entity.setCreateTime(new Date());
        entity.setUpdateTime(new Date());
        this.save(entity);
    }

    @Override
    public void delete(CAdviceFeedbackParam param){
        this.removeById(getKey(param));
    }

    @Override
    public void update(CAdviceFeedbackParam param){
        CAdviceFeedback oldEntity = getOldEntity(param);
        CAdviceFeedback newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public CAdviceFeedbackResult findBySpec(CAdviceFeedbackParam param){
        return null;
    }

    @Override
    public List<CAdviceFeedbackResult> findListBySpec(CAdviceFeedbackParam param){
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(CAdviceFeedbackParam param){
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        List<Long> userIdList=new LinkedList<>();
        List<CAdviceFeedbackResult> cAdviceFeedbackResults=page.getRecords();
        cAdviceFeedbackResults.stream().forEach(cAdviceFeedbackResult -> userIdList.add(cAdviceFeedbackResult.getUserId()));
        Map<Long,String> map=userService.list(new QueryWrapper<User>().in("user_id",userIdList)).stream().collect(Collectors.toMap(User::getUserId,User::getName));
        cAdviceFeedbackResults.forEach(cAdviceFeedbackResult -> {
            if (map.containsKey(cAdviceFeedbackResult.getUserId())){
                cAdviceFeedbackResult.setUserName(map.get(cAdviceFeedbackResult.getUserId()));
            }
        });
        page.setRecords(cAdviceFeedbackResults);
        return LayuiPageFactory.createPageInfo(page);
    }

    private Serializable getKey(CAdviceFeedbackParam param){
        return param.getAdviceId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private CAdviceFeedback getOldEntity(CAdviceFeedbackParam param) {
        return this.getById(getKey(param));
    }

    private CAdviceFeedback getEntity(CAdviceFeedbackParam param) {
        CAdviceFeedback entity = new CAdviceFeedback();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

    /**
     * 查看回复详情
     *
     * @param adviceId
     * @return
     */
    @Override
    public CAdviceFeedbackResult findById(String adviceId) {
        CAdviceFeedback cAdviceFeedback=getById(adviceId);
        CAdviceFeedbackResult result = new CAdviceFeedbackResult();
        ToolUtil.copyProperties(cAdviceFeedback, result);
        return result;
    }
}
